//
//  RSVisitorController.swift
//  RSWeibo
//
//  Created by Macx on 16/8/8.
//  Copyright © 2016年 Macx. All rights reserved.
//

import UIKit
import SVProgressHUD

class RSVisitorController: UITableViewController {
    
    // 用来标记是否已经登录, 默认为没有登录
    /*
     从沙盒中取出access_token, 
     1, 如果access_token 为空, 说明没有登录, 加载的是访客界面
     2, 如果access_token不为空, 说明登录成功了, 就不必加载访客界面了
     */
    var isLogin: Bool = RSUserAccoutViewModel.sharedUserAccountViewModel.userAccountModel?.access_token != nil
    
    // 拥有一个 RSVisitor 视图
    var visitorView: RSVisitor?
    
    
    /*
     如果有可能加载不同的视图, 就在这个方法里面实现
     */
    override func loadView() {
        // 判断, 如果是登录状态, 则加载原来的视图, 如果不是登录状态, 则加载访问者视图
        if isLogin {
            super.loadView()
            
            // 发送通知
//            NSNotificationCenter.defaultCenter().postNotificationName(changeRootViewController, object: "toOtherVC")
            
        }else{
            
            // 加载访问者视图
            visitorView = RSVisitor()
            
            // 完成回调的闭包
            visitorView?.loginClosure = { [weak self] in
                
                // 在闭包中需要使用 self, 弱引用之后 self 变成可选属性, 要在后面加上!或者?
                self?.gotoOauthVC()
                
            }
            
        
            view = visitorView
            
        }

    }
    
    
    override func viewDidLoad() {
        
        super.viewDidLoad()
        
        setupUI()
        
    }
    
    private func setupUI(){
        // 创建导航栏两个两个按钮
       navigationItem.leftBarButtonItem = UIBarButtonItem(title: "注册", style: .Plain, target: self, action: "registlActionClick")
        navigationItem.rightBarButtonItem = UIBarButtonItem(title: "登录", style: .Plain, target: self, action: "loginActionClick")
        
        
    }
    
    // MARK: 点击方法
    @objc private func registlActionClick(){
    
        gotoOauthVC()
        // 转圈图 去掉
        SVProgressHUD.dismiss()
        
    }
    @objc private func loginActionClick(){
        
        gotoOauthVC()
    }
    
    // 跳转到第三方登录界面方法
    private func gotoOauthVC(){
    
        let oauthVC =  RSOauthViewController()
        
        let nav = UINavigationController(rootViewController: oauthVC)
        
        presentViewController(nav, animated: true, completion: nil)
        
        
    }
    

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    // MARK: - Table view data source

    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 0
    }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        return 0
    }

    /*
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("reuseIdentifier", forIndexPath: indexPath)

        // Configure the cell...

        return cell
    }
    */

    /*
    // Override to support conditional editing of the table view.
    override func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
        // Return false if you do not want the specified item to be editable.
        return true
    }
    */

    /*
    // Override to support editing the table view.
    override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
        if editingStyle == .Delete {
            // Delete the row from the data source
            tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
        } else if editingStyle == .Insert {
            // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
        }    
    }
    */

    /*
    // Override to support rearranging the table view.
    override func tableView(tableView: UITableView, moveRowAtIndexPath fromIndexPath: NSIndexPath, toIndexPath: NSIndexPath) {

    }
    */

    /*
    // Override to support conditional rearranging of the table view.
    override func tableView(tableView: UITableView, canMoveRowAtIndexPath indexPath: NSIndexPath) -> Bool {
        // Return false if you do not want the item to be re-orderable.
        return true
    }
    */

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
